/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.mleiria.generator;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.security.InvalidKeyException;
import java.security.cert.Certificate;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;

/**
 *
 * @author NB13712
 */
public class SigGen {

    private String options;
    private final String storePass;
    private String keystoreFile;
    private String signatureFile;
    private Signature sig;
    private KeyStore keystore;

    public SigGen(final pt.mleiria.algorithm.Signature algSig, 
            final String keystoreDile, final String storePass)
                throws KeyStoreException, FileNotFoundException, IOException, 
                NoSuchAlgorithmException, CertificateException {
        
        this.storePass = storePass;
        this.sig = Signature.getInstance(algSig.value());
        this.keystore = KeyStore.getInstance(KeyStore.getDefaultType());
        keystore.load(new FileInputStream(keystoreFile), storePass.toCharArray());

    }

    /**
     * The password for the private key is assumed to be the same as
     * the keystore password
     * @param alias
     * @return
     * @throws KeyStoreException
     * @throws NoSuchAlgorithmException
     * @throws UnrecoverableKeyException
     * @throws InvalidKeyException 
     */
    public boolean signData(final String alias)
            throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException, InvalidKeyException {

        final Key key = keystore.getKey(alias, storePass.toCharArray());
        sig.initSign((PrivateKey)key);
        
        /*
        if (key instanceof PrivateKey) {
            // Get certificate of public key
            Certificate cert = keystore.getCertificate(alias);
            // Get public key
            PublicKey publicKey = cert.getPublicKey();
            // Return a key pair
            new KeyPair(publicKey, (PrivateKey) key);
        }
       */
        

        return false;
    }
    
    public boolean verifyData(final String alias, final String storePass) throws InvalidKeyException, KeyStoreException{
        
        sig.initVerify(keystore.getCertificate(alias).getPublicKey());
        
        return false;
    }
    
    private void updateSig(final String messageFile, final Signature sig) 
            throws FileNotFoundException, IOException, SignatureException{
        
        final File f = new File(messageFile);
        final FileInputStream in = new FileInputStream(f);
        final FileChannel fchannel = in.getChannel();
        final ByteBuffer bytebuf = ByteBuffer.allocate((int)f.length());
        fchannel.read(bytebuf);
        sig.update(bytebuf);
        
        
    }

}
